# coding=utf-8

"""
@Author: LiangChao
@Email: kevinleong1011@hotmail.com
@Desc: 
"""
import inspect
from functools import wraps


class UnittestCase:
    """对传统自动化用例的支持"""

    def __getattribute__(self, item):
        v = super().__getattribute__(item)
        if not item.startswith('_') and inspect.ismethod(v):
            try:
                setup_method = object.__getattribute__(self, 'setup_method')
                setup_method()
            except AttributeError:
                pass

            try:
                teardown_method = object.__getattribute__(self, 'teardown_method')
            except AttributeError:
                teardown_method = None

            @wraps(v)
            def deco(*args, **kwargs):
                try:
                    return v(*args, **kwargs)
                finally:
                    if teardown_method:
                        teardown_method()

            return deco
        return v
